package com.gs.spring.core.aop.pointcut;

import java.util.Date;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class LoggingAspect {

	//All methods defined in the service package
	//Before("execution(* com..service.*.*(..))") //there are several directories between com and service
	//Before("execution(* com.gs.spring.*.service.*.*(..))")  //there is one directory between spring & service
	//Before("execution (* *..service.*.*(..))") //any package or sub-package called service
	//Before("execution (* *..util..*.*(..))") //any package or sub-package called util  "execution (* *..util.*.*(..) will not work
	//Before("execution (* com.gs.spring.core.service.PatientService+.*(..))") //any method of type or subtype of PatientService
	@Before(" execution(@com.gs.spring.util.RolesAllowed * *(..)) ") //any method annotated with @RolesAllowed
	public void logServiceMessage(JoinPoint jp){
		String name = jp.getSignature().getName();
		System.out.println("method "+name+" in use of class " + jp.getTarget().getClass().getName() + " " + new Date());
	}
	
	
	@Before("execution(* com.gs.spring.core.service.PatientService+.findAll(..))")
	public void logMessage(JoinPoint jp){
		String name = jp.getSignature().getName();
		System.out.println("dasda method "+name+" in use " + jp.getClass().getName());
	}
	//Before("execution(* com.gs.spring.core.service.PatientService.*(..))")
	public void logMessageAnyPatientService(JoinPoint jp){
		String name = jp.getSignature().getName();
		System.out.println("method "+name+" in use at logMessageAnyPatientService");
	}
	//Before("execution(* com.gs.spring.core.service.PatientService.s*(..))")
	public void logMessageAnyPatientServiceSave(JoinPoint jp){
		String name = jp.getSignature().getName();
		System.out.println("method "+name+" in use at logMessageAnyPatientService");
	}
	
	//Before("execution(void set*(*))")	
	public void logMessageSetters(JoinPoint jp){
		String name = jp.getSignature().getName();
		System.out.println("**************************method "+name+" in use");
	}
	//Before("execution(void set*(*))")
	public void trackChange(JoinPoint jp){
		
		String name = jp.getSignature().getName();
		System.out.println("name :  " + name);
		Object newValue = jp.getArgs()[0];
		System.out.println("About to change to " + newValue + " on " + jp.getThis());
		
	}	
}
